package com.stedu.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.stedu.bean.RespBean;
import com.stedu.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.UUID;

@RestController
public class AuthController {
    @Autowired
    private RedisUtil redisUtil;
    @GetMapping("/captcha")
    public RespBean captcha(){
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(120, 38, 4, 10);
        //生成uuid
        String key = UUID.randomUUID().toString().replace("-", "");
        redisUtil.set(key, lineCaptcha.getCode());
        redisUtil.expire(key, 120);//设置redis中存放的验证码的超时时间
        String imageBase64Data = lineCaptcha.getImageBase64Data();
        HashMap<String, Object> map = new HashMap<>();
        map.put("key",key);
        map.put("imageBase64Data", imageBase64Data);
        return RespBean.ok("",map);
    }
}
